/*!
    This file is part of KapiSkript.

    Copyright (C) 2009 RaNaN

    KapiSkript is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
*/

function kl_setString(prefs, name, str) {
  var supports_str = Components.classes["@mozilla.org/supports-string;1"]
      .createInstance(Components.interfaces.nsISupportsString);
  supports_str.data = str;
  prefs.setComplexValue(name, Components.interfaces.nsISupportsString, supports_str);
}

function kl_getString(prefs, name) {
  return prefs.getComplexValue(name, Components.interfaces.nsISupportsString).data;
}


function kl_InitOptions(){
            
    //	document.getElementById("overview").label = unescape("%DCbersicht");
    document.getElementById("grphaut").addEventListener("click", kl_graph,true);
    document.getElementById("statsakt").addEventListener("click", kl_stats,true);

    document.getElementById("sigvorakt").addEventListener("click", kl_sig,true);
    document.getElementById("signachakt").addEventListener("click", kl_sig,true);
    
    //var pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
    try {

        var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

        document.getElementById("endzeit").checked = prefManager.getBoolPref("extensions.ks.endzeit");
        document.getElementById("geldsumme").checked = prefManager.getBoolPref("extensions.ks.geldsumme");
        document.getElementById("produktion").checked = prefManager.getBoolPref("extensions.ks.produktion");
        document.getElementById("umsatzverkauf").checked = prefManager.getBoolPref("extensions.ks.umsatzverkauf");
        document.getElementById("umsatzlager").checked = prefManager.getBoolPref("extensions.ks.umsatzlager");
        document.getElementById("countdown").checked = prefManager.getBoolPref("extensions.ks.countdown");
        document.getElementById("ausgabenmarkt").checked = prefManager.getBoolPref("extensions.ks.ausgabenmarkt");
        document.getElementById("allewaren").checked = prefManager.getBoolPref("extensions.ks.allewaren");
        document.getElementById("ausbau").checked = prefManager.getBoolPref("extensions.ks.ausbau");

        document.getElementById("gesamtwert").checked = prefManager.getBoolPref("extensions.ks.gesamtwert");
        document.getElementById("sortierung").selectedIndex = prefManager.getIntPref("extensions.ks.sortierung");
        document.getElementById("welt").selectedIndex = prefManager.getIntPref("extensions.ks.welt");
        document.getElementById("schnellzugriff").checked = prefManager.getBoolPref("extensions.ks.schnellzugriff");
        document.getElementById("marktskip").checked = prefManager.getBoolPref("extensions.ks.marktskip");
        document.getElementById("vertragskip").checked = prefManager.getBoolPref("extensions.ks.vertragskip");
        document.getElementById("prodskip").checked = prefManager.getBoolPref("extensions.ks.prodskip");
        document.getElementById("firmensuche").checked = prefManager.getBoolPref("extensions.ks.firmensuche");
        document.getElementById("sortadress").checked = prefManager.getBoolPref("extensions.ks.sortadress");
        document.getElementById("nachrichtskip").checked = prefManager.getBoolPref("extensions.ks.nachrichtskip");
        document.getElementById("forschend").checked = prefManager.getBoolPref("extensions.ks.forschend");
        document.getElementById("gebfast").checked = prefManager.getBoolPref("extensions.ks.gebfast");
        document.getElementById("wb").checked = prefManager.getBoolPref("extensions.ks.wb");
        document.getElementById("gebover").checked = prefManager.getBoolPref("extensions.ks.gebover");
        document.getElementById("gebsort").selectedIndex = prefManager.getIntPref("extensions.ks.geb2sort");
        document.getElementById("nbopac").value = prefManager.getIntPref("extensions.ks.nbopac");
        document.getElementById("notizbuch").checked = prefManager.getBoolPref("extensions.ks.notizbuch");
        document.getElementById("todo").checked = prefManager.getBoolPref("extensions.ks.todo");
        document.getElementById("verwaltsort").checked = prefManager.getBoolPref("extensions.ks.verwaltsort");
        document.getElementById("statsakt").checked = prefManager.getBoolPref("extensions.ks.statsakt");
        document.getElementById("statsdiff").checked = prefManager.getBoolPref("extensions.ks.statsdiff");
        document.getElementById("statscol").checked = prefManager.getBoolPref("extensions.ks.statscol");
        document.getElementById("statsverkaufcol").checked = prefManager.getBoolPref("extensions.ks.statsverkaufcol");
        document.getElementById("grphaut").checked = prefManager.getBoolPref("extensions.ks.grphaut");
        document.getElementById("grph1").selectedIndex = prefManager.getIntPref("extensions.ks.grph1");
        document.getElementById("grph2").selectedIndex = prefManager.getIntPref("extensions.ks.grph2");
        document.getElementById("grphstat").checked = prefManager.getBoolPref("extensions.ks.grphstat");
        document.getElementById("grphverk").checked = prefManager.getBoolPref("extensions.ks.grphverk");
        document.getElementById("verwaltbereit").checked = prefManager.getBoolPref("extensions.ks.verwaltbereit");
        document.getElementById("menupreis").checked = prefManager.getBoolPref("extensions.ks.menupreis");
        document.getElementById("spqchoose").checked = prefManager.getBoolPref("extensions.ks.spqchoose");
        document.getElementById("vkpremium").checked = prefManager.getBoolPref("extensions.ks.vkpremium");
        document.getElementById("nbcols").value = prefManager.getIntPref("extensions.ks.nbcols");
        document.getElementById("nbrows").value = prefManager.getIntPref("extensions.ks.nbrows");

        document.getElementById("igmmarkt").checked = prefManager.getBoolPref("extensions.ks.igmmarkt");
        document.getElementById("igmfax").checked = prefManager.getBoolPref("extensions.ks.igmfax");
        document.getElementById("igmadress").checked = prefManager.getBoolPref("extensions.ks.igmadress");
        document.getElementById("sigvorakt").checked = prefManager.getBoolPref("extensions.ks.sigvorakt");
        document.getElementById("signachakt").checked = prefManager.getBoolPref("extensions.ks.signachakt");
        document.getElementById("sigvor").value = kl_getString(prefManager,"extensions.ks.sigvor");
        document.getElementById("signach").value = kl_getString(prefManager,"extensions.ks.signach");
        document.getElementById("saveq").checked = prefManager.getBoolPref("extensions.ks.saveq");
        document.getElementById("sortvertrag").checked = prefManager.getBoolPref("extensions.ks.sortvertrag");
        document.getElementById("igmvertrag").checked = prefManager.getBoolPref("extensions.ks.igmvertrag");
        document.getElementById("alleverk").checked = prefManager.getBoolPref("extensions.ks.alleverk");

        document.getElementById("ausbaupa").checked = prefManager.getBoolPref("extensions.ks.ausbaupa");
        document.getElementById("quicklinks").checked = prefManager.getBoolPref("extensions.ks.quicklinks");
        document.getElementById("prodanz").checked = prefManager.getBoolPref("extensions.ks.prodanz");
        document.getElementById("inputcalc").checked = prefManager.getBoolPref("extensions.ks.inputcalc");


        var zeit = prefManager.getCharPref("extensions.ks.zeiten");
        zeit = zeit.split(":");
        zeit.pop();
        var i;
        for (i = 0; i < zeit.length;i++) {
            document.getElementById("zeit" + i).value = zeit[i];
        }

        var farbe = prefManager.getCharPref("extensions.ks.farben");
        farbe = farbe.split(":");
        farbe.pop();
        for (i = 0; i < farbe.length;i++) {
            document.getElementById("farbe" + i).value = farbe[i];
        }
        kl_graph();
        kl_stats();
        kl_sig();

    } catch(e) {
        alert("Konnte Einstellungen nicht laden.\n" + e);
    }
	
}

function kl_SaveOptions(){

    try{
        var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

        var zeit = "";
        var farbe = "";

        for (var i = 0; i < 4; i++) {
            zeit += parseFloat(document.getElementById("zeit"+i).value)+":";
        }
        prefManager.setCharPref("extensions.ks.zeiten",zeit);

        for (i= 0; i < 4;i++){
            document.getElementById("farbe"+i).value = document.getElementById("farbe"+i).value.replace(/[#]/g,"");

            if(document.getElementById(("farbe"+i)).value.match(/[A-Fa-f0-9]{6}/) && document.getElementById(("farbe"+i)).value.length == 6){
                farbe += document.getElementById("farbe"+i).value + ":";
            }else{
                alert("Keine gültige Farbe\nEine Farbe muss 6 Zeichen haben und nur aus 0-9 und a-f bestehen.");
                return false;
            }

        }
        prefManager.setCharPref("extensions.ks.farben",farbe);
        if(document.getElementById("statsdiff").checked != true && document.getElementById("statscol").checked != true) document.getElementById("statsakt").checked = false;



        prefManager.setBoolPref("extensions.ks.endzeit", document.getElementById("endzeit").checked);
        prefManager.setBoolPref("extensions.ks.geldsumme", document.getElementById("geldsumme").checked);
        prefManager.setBoolPref("extensions.ks.produktion", document.getElementById("produktion").checked);
        prefManager.setBoolPref("extensions.ks.umsatzverkauf", document.getElementById("umsatzverkauf").checked);
        prefManager.setBoolPref("extensions.ks.umsatzlager", document.getElementById("umsatzlager").checked);
        prefManager.setBoolPref("extensions.ks.countdown", document.getElementById("countdown").checked);
        prefManager.setBoolPref("extensions.ks.ausgabenmarkt", document.getElementById("ausgabenmarkt").checked);
        prefManager.setBoolPref("extensions.ks.allewaren", document.getElementById("allewaren").checked);
        prefManager.setBoolPref("extensions.ks.ausbau", document.getElementById("ausbau").checked);
        prefManager.setBoolPref("extensions.ks.gesamtwert", document.getElementById("gesamtwert").checked);
        prefManager.setIntPref("extensions.ks.sortierung", parseInt(document.getElementById("sortierung").selectedIndex));
        prefManager.setIntPref("extensions.ks.welt", parseInt(document.getElementById("welt").selectedIndex));
        prefManager.setBoolPref("extensions.ks.schnellzugriff", document.getElementById("schnellzugriff").checked);
        prefManager.setBoolPref("extensions.ks.marktskip", document.getElementById("marktskip").checked);
        prefManager.setBoolPref("extensions.ks.vertragskip", document.getElementById("vertragskip").checked);
        prefManager.setBoolPref("extensions.ks.prodskip", document.getElementById("prodskip").checked);
        prefManager.setBoolPref("extensions.ks.firmensuche", document.getElementById("firmensuche").checked);
        prefManager.setBoolPref("extensions.ks.sortadress", document.getElementById("sortadress").checked);
        prefManager.setBoolPref("extensions.ks.nachrichtskip", document.getElementById("nachrichtskip").checked);
        prefManager.setBoolPref("extensions.ks.forschend", document.getElementById("forschend").checked);
        prefManager.setBoolPref("extensions.ks.gebfast", document.getElementById("gebfast").checked);
        prefManager.setBoolPref("extensions.ks.wb", document.getElementById("wb").checked);
        prefManager.setBoolPref("extensions.ks.gebover", document.getElementById("gebover").checked);
        prefManager.setIntPref("extensions.ks.geb2sort", parseInt(document.getElementById("gebsort").selectedIndex));
        prefManager.setIntPref("extensions.ks.nbopac", parseInt(document.getElementById("nbopac").value));
        prefManager.setBoolPref("extensions.ks.notizbuch", document.getElementById("notizbuch").checked);
        prefManager.setBoolPref("extensions.ks.todo", document.getElementById("todo").checked);
        prefManager.setBoolPref("extensions.ks.verwaltsort", document.getElementById("verwaltsort").checked);
        prefManager.setBoolPref("extensions.ks.statsakt", document.getElementById("statsakt").checked);
        prefManager.setBoolPref("extensions.ks.statsdiff", document.getElementById("statsdiff").checked);
        prefManager.setBoolPref("extensions.ks.statscol", document.getElementById("statscol").checked);
        prefManager.setBoolPref("extensions.ks.statsverkaufcol", document.getElementById("statsverkaufcol").checked);
        prefManager.setBoolPref("extensions.ks.grphstat", document.getElementById("grphstat").checked);
        prefManager.setBoolPref("extensions.ks.grphverk", document.getElementById("grphverk").checked);
        prefManager.setBoolPref("extensions.ks.grphaut", document.getElementById("grphaut").checked);
        prefManager.setIntPref("extensions.ks.grph1", parseInt(document.getElementById("grph1").selectedIndex));
        prefManager.setIntPref("extensions.ks.grph2", parseInt(document.getElementById("grph2").selectedIndex));
        prefManager.setBoolPref("extensions.ks.verwaltbereit", document.getElementById("verwaltbereit").checked);
        prefManager.setBoolPref("extensions.ks.menupreis", document.getElementById("menupreis").checked);
        prefManager.setBoolPref("extensions.ks.spqchoose", document.getElementById("spqchoose").checked);
        prefManager.setBoolPref("extensions.ks.vkpremium", document.getElementById("vkpremium").checked);
        prefManager.setIntPref("extensions.ks.nbcols", parseInt(document.getElementById("nbcols").value));
        prefManager.setIntPref("extensions.ks.nbrows", parseInt(document.getElementById("nbrows").value));
        prefManager.setBoolPref("extensions.ks.igmmarkt", document.getElementById("igmmarkt").checked);
        prefManager.setBoolPref("extensions.ks.igmfax", document.getElementById("igmfax").checked);
        prefManager.setBoolPref("extensions.ks.igmadress", document.getElementById("igmadress").checked);
        prefManager.setBoolPref("extensions.ks.sigvorakt", document.getElementById("sigvorakt").checked);
        prefManager.setBoolPref("extensions.ks.signachakt", document.getElementById("signachakt").checked);
        kl_setString(prefManager,"extensions.ks.sigvor", document.getElementById("sigvor").value);
        kl_setString(prefManager,"extensions.ks.signach", document.getElementById("signach").value);
        prefManager.setBoolPref("extensions.ks.saveq", document.getElementById("saveq").checked);
        prefManager.setBoolPref("extensions.ks.sortvertrag", document.getElementById("sortvertrag").checked);
        prefManager.setBoolPref("extensions.ks.igmvertrag", document.getElementById("igmvertrag").checked);
        prefManager.setBoolPref("extensions.ks.alleverk", document.getElementById("alleverk").checked);
        prefManager.setBoolPref("extensions.ks.prodanz", document.getElementById("prodanz").checked);


        prefManager.setBoolPref("extensions.ks.ausbaupa", document.getElementById("ausbaupa").checked);
        prefManager.setBoolPref("extensions.ks.quicklinks", document.getElementById("quicklinks").checked);
        prefManager.setBoolPref("extensions.ks.inputcalc", document.getElementById("inputcalc").checked);

    }catch(e){
        alert("Konnte Einstellungen nicht speichern.\n"+ e); return false;
    }
	
    return true;
	
}
function kl_graph(){
    if(document.getElementById("grphaut").checked){
        document.getElementById("grph1").disabled = true;
        document.getElementById("grph2").disabled = true;
    }else{
        document.getElementById("grph1").disabled = false;
        document.getElementById("grph2").disabled = false;
    }
    }
function kl_stats(){
    if(document.getElementById("statsakt").checked){
        document.getElementById("statsdiff").disabled = false;
        document.getElementById("statscol").disabled = false;
    }else{
        document.getElementById("statsdiff").disabled = true;
        document.getElementById("statscol").disabled = true;
    }

}
function kl_sig(){
    if(document.getElementById("sigvorakt").checked){
        document.getElementById("sigvor").disabled = false;
    }else{
        document.getElementById("sigvor").disabled = true;
    }
    if(document.getElementById("signachakt").checked){
        document.getElementById("signach").disabled = false;
    }else{
        document.getElementById("signach").disabled = true;
    }

}
